package cn.abcsys.cloud.devops.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.abcsys.cloud.devops.web.entity.User;
import cn.abcsys.cloud.devops.web.ldap.ConfigureCommon;
import cn.abcsys.cloud.devops.web.util.HttpClient;


@Controller
@PropertySource("classpath:config/system.properties")
public class ForwardController {
	private static final Logger LOGGER = Logger.getLogger(ForwardController.class);
	ConfigureCommon cfg = new ConfigureCommon("config/system.properties");
	private static final String SUCCESS = "success";
	private static final String MESSAGE = "message";
	/**
	 * application,deployer,runtime,monitor
	 * post请求重定向
	 * xyj style
	 */
	@RequestMapping(value = {"{serviceName}/{[a-zA-Z-]+}.do","{serviceName:runtime}/{[a-z-]+}/{[a-z-]+}","{serviceName:pipeline}/{[a-z-]+}/{[a-z-]+}","{serviceName:monitor}/{[a-z-]+}/{[a-z-]+}","{serviceName:logger}/{[a-z-]+}"}, method = { RequestMethod.POST })
	@ResponseBody
	public JSONObject hostPostReq(HttpServletRequest request,@PathVariable String serviceName,@RequestBody Map<String,String> param){
		//获取访问的url
		String path = request.getServletPath();
		//通过URL中服务名称获取真实URL链接   http:ip:port/项目名
		HttpSession session = request.getSession();
		if(null == session.getAttribute("areaFlag"))
			return null;
		String areFlag = (String) session.getAttribute("areaFlag");
		String baseUrl = null; 
		//根据前端表示0 北京1苏州，区分对应的真实url
		if("0".equals(areFlag)){
			 baseUrl=cfg.getKey("service."+serviceName+0);
		}else if("1".equals(areFlag)){
			 baseUrl=cfg.getKey("service."+serviceName+1);
		}else{
			return null;
		}
		//String baseUrl=cfg.getKey("service."+serviceName);
		LOGGER.info("serviceName:"+baseUrl);
		//拼接真实访问url
		String lastUrl=path.substring(serviceName.length()+1);
		User user = (User)session.getAttribute("user");
		Integer envId = (Integer)session.getAttribute("envId");
		String areaFlag = (String)session.getAttribute("areaFlag");
		if(null==envId&&!serviceName.equals("runtime")){
			JSONObject responseMap = new JSONObject();
			responseMap.put(SUCCESS, false);
			responseMap.put(MESSAGE,"请选择环境");
			return responseMap;
		}
		if(null==areaFlag){
			JSONObject responseMap = new JSONObject();
			responseMap.put(SUCCESS, false);
			responseMap.put(MESSAGE,"请选择区域");
			return responseMap;
		}
		if(!lastUrl.equals("/logger")){
			param.put("userId", String.valueOf(user.getUserId()));

			param.put("envId", envId==null?"0":String.valueOf(envId));
			param.put("areaFlag", areaFlag);
		}
		//请求接口
		JSONObject result=HttpClient.doGet(param, baseUrl+lastUrl);
		//返回数据
		return result;
	}
}